<template>
  <div>
    <el-dialog
      :visible.sync="dialogRefundVisible"
      :title="type == 1 ? '退款' : '上传售后单'"
      @close="cancel"
    >
      <el-form :model="params" :rules="rules" ref="formRef">
        <el-form-item label="数量" label-width="100px" prop="number">
          <el-input
            type="number"
            :min="1"
            :max="maxNumber"
            :placeholder="type == 1 ? '请输入退款数量' : '请输入上传售后单数量'"
            v-model="params.number"
            size="small"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="cancel" size="mini">取消</el-button>
        <el-button type="primary" @click="submit" size="mini">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { refund, send } from "@/api/behalf"
export default {
  name: "refund",
  data() {
    return {
      dialogRefundVisible: false,
      maxNumber: 0,
      type: 1,
      params: {
        behalf_product_id: "",
        number: 0,
      },
      rules: {
        number: [
          { required: true, message: "请输入退款数量", trigger: "blur" },
        ],
      },
    }
  },
  methods: {
    open(type = 1, e) {
      this.type = type
      this.maxNumber = Number(e.product_number) - Number(e.refund_number)
      this.params.behalf_product_id = e.id
      this.dialogRefundVisible = true
    },
    cancel() {
      this.params.behalf_product_id = ""
      this.params.number = 0
      this.dialogRefundVisible = false
      this.$refs.formRef.resetFields()
    },
    submit() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          let url = this.type === 1 ? refund(this.params) : send(this.params)
          try {
            const res = await url
            this.$msg.successMsg(res.msg)
            this.cancel()
          } catch (ex) {
            console.log(ex)
          }
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
